package Method;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/8/23 15:06
 */
public class MethodHomeWork {
    public static void main(String[] args) {
//        for (int i = 1; i < 11; i++) {
//            System.out.print(fiboD(i)+"、");
//        }
//        System.out.println(factor(5));
//        System.out.println(numSum(10));
//        print(1234);
//        System.out.println(bitSum(12345));
//        System.out.println(fiboD(0));

        hanoiTower( 3, 'A','B','C');
    }

    //求斐波那契数列的第n项。(迭代实现)
    public static int fibo(int num){
        int f1=0,f2=1;
        int ret =0;
        if(num==2){
            return 1;
        }
        for (int i = 3; i <= num; i++) {
            ret = f1+f2;
            f1=f2;
            f2=ret;
        }
        return ret;
    }
    //递归求 N 的阶乘
    public static int factor(int num){
        if(num == 1){
            return 1;
        }
        return num * factor(num-1);
    }
    //递归求 1 + 2 + 3 + ... + 10
    public static int numSum(int num){
        if(num == 1){
            return 1;
        }
        return num + numSum(num-1);
    }
    //按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4) （递归）
    public static void print(int num){
        if (num<10){
            System.out.print(num+" ");
            return;
        }
        print(num/10);
        System.out.print(num%10+" ");
    }
    //写一个递归方法，输入一个非负整数，返回组成它的数字之和  123=1+2+3
    public static int bitSum(int num){
        if(num<10){
            return num;
        }
        return num%10 + bitSum(num/10);
    }
    //递归求斐波那契数列的第 N 项
    public static int fiboD(int num){
        if (num==1){
            return 0;
        }
        if(num ==2||num==3){
            return 1;
        }
        return fiboD(num-1)+fiboD(num-2);
    }
    //递归求解汉诺塔问题
    public static void hanoiTower(int Disks,char A,char B,char C) {
        if (Disks==1){
            move(Disks,A,C);
            return;
        }
        hanoiTower(Disks-1,A,C,B);
        move(Disks,A,C);
        hanoiTower(Disks-1,B,A,C);
    }
    public static void move(int nDisks,char sour,char dest){
        System.out.println("正在将编号为" + nDisks + "的盘子从" +sour + "->" + dest);
    }

}
